import {useAsyncStorage} from '@react-native-community/async-storage';
const useStorage = <T>(key: string) => {
  const AsyncStorage = useAsyncStorage(key);
  const setItem = (value: any, callback?: any) => {
    return AsyncStorage.setItem(JSON.stringify(value), callback);
  };
  const getItem = (callback?: any): Promise<T | null> => {
    return new Promise(resolve => {
      AsyncStorage.getItem(callback).then(res => {
        resolve(res ? JSON.parse(res) : null);
      });
    });
  };
  const clear = () => {
    return AsyncStorage.removeItem();
  };
  return {
    setItem,
    getItem,
    clear,
  };
};
export default useStorage;
